<template>
  <div class="container-water-fall water-content">
    <waterfall
      :col="col"
      :data="imgsArr"
    >
      <div
        v-for="(item,index) in imgsArr"
        :key="index"
        class="cell-item"
      >
        <nuxt-link v-if="item.type==='moment'" :to="`/moment/${item.id}`">
          <img
            v-if="item.img"
            :src="item.img"
            :lazy-src="item.img"
            alt="加载错误"
            class="fallEasy-img"
          >
        </nuxt-link>
        <nuxt-link v-if="item.type==='article'" :to="`/article/${item.id}`">
          <img
            v-if="item.img"
            :src="item.img"
            :lazy-src="item.img"
            alt="加载错误"
            class="fallEasy-img"
          >
        </nuxt-link>
        <div
          v-if="item.text"
          class="fallEasy-wrap"
        >
          <nuxt-link v-if="item.type==='moment'" :to="`/moment/${item.id}`">
            <div class="fallEasy-text">
              {{ item.text }}
            </div>
          </nuxt-link>
          <nuxt-link v-if="item.type==='article'" :to="`/article/${item.id}`">
            <div class="fallEasy-text">
              {{ item.text }}
            </div>
          </nuxt-link>

          <div class="fallEasy-user-wrap">
            <div class="user">
              <div
                :style="{background:'url('+ item.user.user_photo+') center center no-repeat',backgroundSize:'100% 100%'}"
                class="user-photo"
              />
              <div class="user-name">
                {{ item.user.user_name }}
              </div>
            </div>
            <div
              v-if="item.star_count!==undefined"
              class="help"
            >
              <div
                :class="[item.is_star===0?'help-icon':'help-icon-active']"
                @click="clickStar(item)"
              />
              <div class="help-text">
                {{ item.star_count }}
              </div>
            </div>
            <div
              v-else
              class="browse"
            >
              浏览 {{ item.browse_count }}
            </div>
          </div>
        </div>
      </div>
    </waterfall>
  </div>
</template>
<script>
// import { setStar, unStar } from '@/api/home'
export default {
  props: {
    datas: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      imgsArr: [],
      col: 4,
      hospitalId: 0
    }
  },
  computed: {
  },
  watch: {
    datas(newValue) {
      this.Datafilter(newValue)
    }
  },
  created() {
    if (this.datas.length) {
      this.Datafilter(this.datas)
    }
  },
  mounted() {
    this.gutterWidth()
  },
  methods: {
    itemWidth() {
      return (138 * 0.5 * (document.documentElement.clientWidth / 375))
    },
    gutterWidth() {
      return (9 * 0.5 * (document.documentElement.clientWidth / 375))
    },
    // 瀑布流数据过滤
    Datafilter(data) {
      const fallEasyDatas = []
      data.forEach(item => {
        switch (item.content_type) {
          // 动态
          case 'moment':
            if (item.content.images.length || item.content.videos.length) {
              fallEasyDatas.push({
                id: item.content.moment_id,
                img: item.content.videos.length ? item.content.videos[0].preview_cover_url : item.content.cover.thumbnail_url,
                info: '',
                name: '',
                text: item.content.moment_content,
                user: item.user,
                star_count: item.star_count,
                type: item.content_type,
                is_star: item.is_star,
                browse_count: item.browse_count
              })
            }
            break
          // 文章
          case 'article':
            if (item.content.images.length) {
              fallEasyDatas.push({
                id: item.content.article_id,
                img: item.content.videos.length ? item.content.videos[0].preview_cover_url : item.content.cover.thumbnail_url,
                info: '',
                name: '',
                text: item.content.article_title,
                user: item.user,
                star_count: item.star_count,
                type: item.content_type,
                is_star: item.is_star,
                browse_count: item.browse_count
              })
            }
            break
        }
      })
      this.imgsArr = fallEasyDatas
    },
    million(value) { // 过万处理
      let num
      if (value > 9999) { // 大于9999显示x.xx万
        num = (Math.floor(value / 1000) / 10) + '万'
      } else if (value < 9999 && value > -9999) {
        num = value
      } else if (value < -9999) { // 小于-9999显示-x.xx万
        num = -(Math.floor(Math.abs(value) / 1000) / 10) + '万'
      }
      return num
    },
    setAlert(i) {
      this.hospitalId = i.hospital.user_id
      this.$store.dispatch('setHospitalAlert')
    },
    // 点赞处理
    clickStar(value) {
      // 调用接口点赞
      // if (value.is_star === 0) {
      //   setStar(value.type, value.id).then(res => {
      //     value.is_star = 1
      //     value.star_count = value.star_count + 1
      //   })
      // } else {
      //   unStar(value.type, value.id).then(res => {
      //     value.is_star = 0
      //     value.star_count = value.star_count - 1
      //   })
      // }
    }
  }
}
</script>
<style lang='scss'>
.container-water-fall {
  box-sizing: border-box;
  .cell-item {
    width: 290px;
    background: #ffffff;
    overflow: hidden;
    box-sizing: border-box;
    border-radius: 6px;
    margin-top: 10px;
    img {
      width: 290px;
      height: auto;
      display: block;
    }
  }
}
.fallEasy-wrap {
  padding: 8px;
  .fallEasy-text {
    font-size: 14px;
    font-weight: 400;
    color: rgba(51, 51, 51, 1);
    line-height: 24px;
  }
  .fallEasy-text:hover{
    cursor: pointer;
  }
  .fallEasy-user-wrap {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 10px;
    .user {
      display: flex;
      align-items: center;
      .user-photo {
        width: 26px;
        height: 26px;
        border-radius: 50%;
      }
      .user-name {
        font-size: 14px;
        font-weight: 400;
        color: rgba(153, 153, 153, 1);
        line-height: 20px;
        margin-left: 8px;
      }
    }
    .help {
      display: flex;
      align-items: center;
      .help-icon {
        width: 14px;
        height: 14px;
        background: url("~assets/img/heart.png") center center no-repeat;
        background-size: 100% 100%;
      }
      .help-icon-active {
        width: 14px;
        height: 14px;
        // background: url("~assets/img/help_active.png") center center no-repeat;
        // background-size: 100% 100%;
      }
      .help-text {
        font-size: 10px;
        font-weight: 400;
        color: rgba(153, 153, 153, 1);
        margin-left: 5px;
      }
    }
    .browse {
      font-size: 10px;
      font-weight: 400;
      color: rgba(153, 153, 153, 1);
      line-height: 0.14rem;
    }
  }
}
.fallEasy-img:hover{
  cursor: pointer;
}
</style>
